﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
//using CrazyWorship1.Windows;

using System.Data;
using System.Reflection;
using CrazyWorship1.ListenerService;
using System.IO;
using System.Net.Sockets;
using System.Windows.Markup;
using CrazyWorship1.Properties;
using System.Xml;

namespace CrazyWorship1
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    sealed public partial class Main : Window
    {
        public Main()
        {
            InitializeComponent();
            if (!Settings.Default.IsDeveloper)
                this.WindowState = WindowState.Maximized;
            else
            {
                this.Top = 0;
                this.Left = 160;
                this.Width = 800;
                this.Height = 550;//TODO: SET MAX

            }

        }
        Sanford.Multimedia.Midi.InputDevice dev;
        Dictionary<string, DateTime> songFilesLastUpdated = new Dictionary<string, DateTime>();
        ClientManager clientManager;

        AssetManager assetManager;
        private void SetupScreens_Click(object sender, RoutedEventArgs e)
        {
            ClientManagerWindow w = new ClientManagerWindow();
            w.SetClientManager(clientManager);
            w.Show();
        }

        private void SetupWindows()
        {
            clientManager = new ClientManager();
            assetManager = new AssetManager();
            assetManager.SetClientManager(clientManager);
            songSelectControl.AssetManager = assetManager;
            if (Sanford.Multimedia.Midi.InputDevice.DeviceCount > 0)
            {
                dev = new Sanford.Multimedia.Midi.InputDevice(0);
                dev.ChannelMessageReceived += new EventHandler<Sanford.Multimedia.Midi.ChannelMessageEventArgs>(dev_ChannelMessageReceived);
                dev.StartRecording();
            }
            this.Closed += new EventHandler(Main_Closed);
        }

        void dev_ChannelMessageReceived(object sender, Sanford.Multimedia.Midi.ChannelMessageEventArgs e)
        {
            foreach (BaseListenerClient c in clientManager.ConnectedClients)
            {
                MidiMessage msg = new MidiMessage();

                msg.Channel = e.Message.MidiChannel;
                msg.Data1 = e.Message.Data1;
                msg.Data2 = e.Message.Data2;
                c.SendMidiMessageAsync(msg);
            }
        }
        //void dev_ChannelMessageReceived(object sender, Sanford.Multimedia.Midi.ChannelMessageEventArgs e)
        //{
        //    MidiMessage m = new MidiMessage() { Channel = e.Message.MidiChannel, Data1 = e.Message.Data1, Data2 = e.Message.Data2 };

        //    client.SendMidiMessage(m);
        //}
        void Main_Closed(object sender, EventArgs e)
        {
            clientManager.CloseAll();
        }
        private void CheckForNewVersion()
        {
            VersionHandler.VersionHandlerSoapClient a = new VersionHandler.VersionHandlerSoapClient();
            StatusBarSection1.Content = "Checking for Software Updates...";
            a.IsNewVersionAvailableCompleted += new EventHandler<CrazyWorship1.VersionHandler.IsNewVersionAvailableCompletedEventArgs>(a_IsNewVersionAvailableCompleted);
            a.IsNewVersionAvailableAsync(Assembly.GetExecutingAssembly().GetName().Version.ToString());
        }

        void a_IsNewVersionAvailableCompleted(object sender, CrazyWorship1.VersionHandler.IsNewVersionAvailableCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                string version = e.Result;
                if (version != "")
                {
                    UpdatedVersion a = new UpdatedVersion();
                    a.Version = version;

                    a.ShowDialog();
                }
                else
                {
                    StatusBarSection1.Content = "Software is up to date";
                }
            }
            else
            {
                StatusBarSection1.Content = "Could not check for new versions: " + e.Error.Message;
            }
        }
        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            CheckForNewVersion();
            SetupWindows();
        }
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            AboutBox a = new AboutBox();
            a.Show();
        }
        private void songSelectControl_SongChanged(object sender, RoutedEventArgs e)
        {
            Song s = songSelectControl.GetSelectedSong();
            Controllers.Children.Clear();
            if (s != null)
            {
                int i = 0;
                foreach (string st in s.Controllers)
                {
                    st.Insert(st.IndexOf(" "), "x:SynchronousMode=\"async\" ");
                    XamlReader2 r = new XamlReader2();
                    r.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(r_LoadCompleted);
                    r.Id=i;
                    LyricController a = (LyricController)r.LoadAsync(new XmlTextReader(new StringReader(st)));
                    if (a is LyricController)
                    {
                        ((LyricController)a).ClientManager = this.clientManager;
                        ((LyricController)a).Song = s;
                        ((LyricController)a).Background = Brushes.Red;
                    }
                    a.Height = 200;
                    Controllers.Children.Add(a);
                   
                    i++;
                }
            }
        }

        void r_LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
          
        }
        private void UnloadSong()
        {
            Controllers.Children.Clear();
        }
    }
    public class XamlReader2 : XamlReader
    {
        public Song Song
        {
            get
            {
                return _song;
            }
            set
            {
                _song = value;
            }
        }
        Song _song;
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        int _id;
    }
}
